package org.regotto.basics.dataStruct;

import java.util.Arrays;

/**
 * 使用数组模拟链表
 */
public class SingleLinkedList {

    public int[] e = new int[1000];
    public int[] ne = new int[1000];
    public int h = -1, idx;

    public void H(int x) {
        e[idx] = x; ne[idx] = h; h = idx++;
    }

    public void D(int k) {
        ne[k] = ne[ne[k]];
    }

    public void I(int k, int x) {
        e[idx] = x;
        ne[idx] = ne[k];
        ne[k] = idx++;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        for (int i = h; i != -1; i = ne[i]) res.append(e[i]).append(" ");
        return res.toString();
    }

    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.H(1);
        singleLinkedList.H(2);
        singleLinkedList.H(3);
        System.out.println(singleLinkedList);
        singleLinkedList.I(2, 4);
        System.out.println(singleLinkedList);
        singleLinkedList.D(1);
        System.out.println(singleLinkedList);
    }

}
